package com.tju.resource.oauth.config.components;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.tju.resource.tool.result.ResultEnum;
import com.tju.resource.tool.result.ResultUtil;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author CLS
 * @Date 2022/1/5 13:43
 */
@Component
public class AuthExceptionEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) {
        response.setStatus(HttpStatus.OK.value());
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        try {
            JSONObject result = JSONUtil.parseObj(ResultUtil.error(ResultEnum.IDENTITY_PAST));
            result.remove("count");
            result.remove("data");
            response.getWriter().write(String.valueOf(result));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}